
from itertools import product


def nbl(x, now=("args",), delta=0):
    if delta > 0:
        yield from nbl(x, now + ("op",), delta - 1)
    if x > 0:
        yield from nbl(x - 1, now + ("args",), delta+1)
    if x == delta == 0:
        yield now


def ins(l):
    acc = []
    i = 0
    for x in l:
        if x == "args":
            acc.append([i])
            i += 1
        else:
            acc.append(["+", "-"])
    yield from product(*acc)


def to_tree(l):
    acc = []
    for x in l:
        if x not in list("+-"):
           acc.append(x)
        else:
            b = acc.pop()
            a = acc.pop()
            c = (x, a, b)
            acc.append(c)
    return acc.pop()

def repr2(tree):
    if isinstance(tree, int):
        # ~ return str(tree)
        return '{' + str(tree) + '}'
    else:
        op, a, b = tree
        ra = repr2(a)
        rb = repr2(b)
        if op == '-' and isinstance(b, tuple):
            rb = '(' + rb + ')'
        return f"{ra}{op}{rb}"



def all_exp(n):
    acc = []
    se = set()
    for l in nbl(n):
        for y in ins(l):
            t = to_tree(y)
            s = repr2(t)
            if s not in se:
                se.add(s)
            acc.append(s)
    return acc


thing = "x1 x2 x3 y2 y3 z2 z3".split()
thing += "z22 z23 z32 z33 x22 x23 x32 x33 y22 y23 y32 y33".split()
thing += "x333".split()
thing += "z233 z322 x233 x322 y233 y322".split()


def rot1(l):
    acc = []
    for x, y, z in l:
        acc.append((-y, x, z))
    acc.sort()
    return acc

def rot2(l):
    acc = []
    for x, y, z in l:
        acc.append((-x, -y, z))
    acc.sort()
    return acc

def rot3(l):
    acc = []
    for x, y, z in l:
        acc.append((y, -x, z))
    acc.sort()
    return acc

def flip(l):
    acc = []
    for x, y, z in l:
        acc.append((-x, y, z))
    acc.sort()
    return acc

def flip1(l):
    acc = []
    for x, y, z in l:
        acc.append((y, x, z))
    acc.sort()
    return acc

def flip2(l):
    acc = []
    for x, y, z in l:
        acc.append((x, -y, z))
    acc.sort()
    return acc

def flip3(l):
    acc = []
    for x, y, z in l:
        acc.append((-y, -x, z))
    acc.sort()
    return acc

def std(l):
    temp = l
    for f in [rot1, rot2, rot3, flip, flip1, flip2, flip3]:
        l2 = f(l)
        if l2 < temp:
            temp = l2
    xl, yl, zl = zip(*temp)
    xm = min(xl)
    ym = min(yl)
    zm = min(zl)
    xl = [x - xm - 1 for x in xl]
    yl = [y - ym - 1 for y in yl]
    zl = [z - zm - 1 for z in zl]
    temp = list(zip(xl, yl, zl))
    return temp

from box import ana

el = all_exp(2)
print(el)
print(thing)



input("continue:")
i = 1
pool = set()
data = []


for x in product(thing, thing, thing):
    for e in el:
        print(i, e.format(*x))
        i += 1
        # ~ e = e.format(*x)
        # ~ shape = ana(e)
        # ~ if len(shape) == 0:
            # ~ continue
        # ~ else:
            # ~ shape = tuple(std(shape))
            # ~ if shape not in pool:
                # ~ pool.add(shape)
                # ~ data.append((len(shape), shape, e))
                # ~ print(i, e)


                # ~ i += 1

data.sort()

# ~ fo = open("edit_num_2.txt", 'w')
# ~ for _, shape, e in data:
    # ~ fo.write(e+'\n')
















